package com.xlw.application;

import android.app.Application;
import android.content.Context;
import android.support.v4.util.LruCache;

import com.xlw.model.DaoMaster;
import com.xlw.model.DaoSession;

import java.util.HashMap;

/**
 * Created by xinliwei on 2015/7/4.
 *
 * 全局类
 */
public class XlwApplication extends Application {

    private static XlwApplication instance;
    public static XlwApplication getInstance(){
        return instance;
    }

    LruCache lruCache;
    HashMap<String,Object> map;

    private static DaoMaster daoMaster;
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;

        init();
    }

    protected void init(){

    }

    /**
     * 获得DaoMaster
     *
     * @param context
     * @return
     */
    public static DaoMaster getDaoMaster(Context context){
        if(daoMaster == null){
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context,AppConfig.DATABASE_NAME,null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }

        return daoMaster;
    }

    /**
     * 获得DaoSession
     *
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context){
        if(daoMaster == null){
            daoMaster = getDaoMaster(context);
        }

        daoSession = daoMaster.newSession();

        return daoSession;
    }

    public static DaoMaster getDaoMaster(){
        if(daoMaster == null){
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(XlwApplication.getInstance(),AppConfig.DATABASE_NAME,null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }

        return daoMaster;
    }

    /**
     * 获得DaoSession
     *
     * @param
     * @return
     */
    public static DaoSession getDaoSession(){
        if(daoMaster == null){
            daoMaster = getDaoMaster();
        }

        daoSession = daoMaster.newSession();

        return daoSession;
    }
}
